/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package uk.co.alt236.btlescan.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
import uk.co.alt236.bluetoothlelib.device.adrecord.AdRecord;
import uk.co.alt236.bluetoothlelib.util.AdRecordUtils;
import uk.co.alt236.bluetoothlelib.util.ByteUtils;
import uk.co.alt236.btlescan.ResourceTable;

import java.text.SimpleDateFormat;
import java.util.Collection;

/**
 * DeviceAbilitySlice
 *
 * @author ljx
 * @since 2021-04-29
 */
public class DeviceAbilitySlice extends AbilitySlice {
    private static final String DB = "db";
    private BluetoothLeDevice device;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_device);
        device = intent.getSequenceableParam("device");
        initView();
        initListener();
    }

    private void initView() {
        Text titlebar = (Text) findComponentById(ResourceTable.Id_tv_title);
        titlebar.setText(device.getName());
        Text deviceName = (Text) findComponentById(ResourceTable.Id_device_name);
        Text deviceAddress = (Text) findComponentById(ResourceTable.Id_device_address);
        Text deviceClass = (Text) findComponentById(ResourceTable.Id_device_class);
        deviceName.setText("Device Name：" + device.getName());
        deviceAddress.setText("Device address：" + device.getAddress());
        deviceClass.setText("Device Class：");
        Text majorClass = (Text) findComponentById(ResourceTable.Id_device_major);
        Text services = (Text) findComponentById(ResourceTable.Id_device_services);
        Text state = (Text) findComponentById(ResourceTable.Id_device_state);
        majorClass.setText("Major Class：");
        services.setText("Services：");
        state.setText("Bonding State：");
        Text firstTime = (Text) findComponentById(ResourceTable.Id_rssi_first_time);
        Text rssi = (Text) findComponentById(ResourceTable.Id_rssi_first);
        Text lastTime = (Text) findComponentById(ResourceTable.Id_rssi_last_time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS zzz");
        String sd = sdf.format(device.getFirstTimestamp());
        firstTime.setText("First timestamp：" + sd);
        rssi.setText("First RSSI：" + device.getFirstRssi() + DB);
        String lastsd = sdf.format(device.getTimestamp());
        lastTime.setText("Last timestamp：" + lastsd);
        Text lastRssi = (Text) findComponentById(ResourceTable.Id_rssi_last);
        lastRssi.setText("Last RSSI：" + device.getRssi() + DB);
        Text average = (Text) findComponentById(ResourceTable.Id_rssi_average);
        average.setText("Running Average RSSI：" + device.getRunningAverageRssi() + DB);
        Text scanRecord = (Text) findComponentById(ResourceTable.Id_scan_record);
        scanRecord.setText(ByteUtils.byteArrayToHexString(device.getScanRecord()));
        Text adtitle = (Text) findComponentById(ResourceTable.Id_title);
        Text asstring = (Text) findComponentById(ResourceTable.Id_as_string);
        Text asarray = (Text) findComponentById(ResourceTable.Id_as_array);
        Collection<AdRecord> adRecords = device.getAdRecordStore().getRecordsAsCollection();
        if (adRecords.size() > 0) {
            for (AdRecord record : adRecords) {
                if (record != null) {
                    String title = "#" + record.getType() + " " + record.getHumanReadableType();
                    adtitle.setText(title);
                    asstring.setText("As String：" + AdRecordUtils.getRecordDataAsString(record));
                    asarray.setText("As Array：" + ByteUtils.byteArrayToHexString(record.getData()));
                }
            }
        }
    }

    private void initListener() {
        Button connect = (Button) findComponentById(ResourceTable.Id_connect);
        connect.setClickedListener(v -> {
            Intent in = new Intent();
            in.setParam("device", device);
            present(new DeviceControlAbilitySlice(), in);
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}